package com.hyb.controller;


import com.alibaba.fastjson.JSON;
import com.hyb.entity.Record;
import com.hyb.page.PageReq;
import com.hyb.response.Result;
import com.hyb.service.RecordService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  交往记录Controller
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;

    @RequestMapping(value = "/createRecord",method = RequestMethod.POST)
    @ApiOperation("新建交往记录")
    public String createRecord(@RequestBody Record record){
        recordService.createRecord(record);
        return JSON.toJSONString(new Result().setMessage("保存成功").setCode(200));
    }

    @RequestMapping(value = "/loadRecords",method = RequestMethod.GET)
    @ApiOperation("加载客户交往信息")
    public String loadRecords(Integer limit,Integer current,Integer customerId){
        PageReq records = recordService.getRecords(limit, current, customerId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(records));
    }

    @RequestMapping(value = "/loadRecordByRecordId",method = RequestMethod.GET)
    @ApiOperation("通过交往记录Id加载交往信息")
    public String loadRecordByRecordId(Integer recordId){
        Record recordByRecordId = recordService.getRecordByRecordId(recordId);
        return JSON.toJSONString(new Result().setData(recordByRecordId).setMessage("修改成功").setCode(200));
    }

    @RequestMapping(value = "/updateRecord",method = RequestMethod.PUT)
    @ApiOperation("修改交往记录")
    public String updateRecord(@RequestBody Record record){
        recordService.updateRecord(record);
        return JSON.toJSONString(new Result().setCode(200).setMessage("修改成功"));
    }

    @RequestMapping(value = "/deleteRecordByRecordId",method = RequestMethod.DELETE)
    @ApiOperation("删除交往记录")
    public String deleteRecordByRecordId(Integer recordId){
        recordService.deleteRecordById(recordId);
        return JSON.toJSONString(new Result().setMessage("删除成功").setCode(200));
    }
}
